import { defineStore } from 'pinia'
import { usePreferredDark } from '@vueuse/core'
import { Theme } from '../types'

interface Tool {
  id: string
  name: string
  icon: string
  description: string
  path: string
  isCustom?: boolean
  url?: string
}

export const useThemeStore = defineStore('theme', {
  state: () => ({
    isDark: usePreferredDark().value // 默认跟随系统
  }),

  actions: {
    toggleTheme() {
      this.isDark = !this.isDark
      this.applyTheme()
    },

    initTheme() {
      // 从本地存储读取主题设置
      const savedTheme = localStorage.getItem('theme')
      if (savedTheme) {
        this.isDark = savedTheme === 'dark'
      } else {
        // 如果没有保存的主题设置，则使用系统主题
        this.isDark = usePreferredDark().value
      }
      this.applyTheme()
    },

    applyTheme() {
      // 应用主题
      document.documentElement.classList.toggle('dark', this.isDark)
      localStorage.setItem('theme', this.isDark ? 'dark' : 'light')
    }
  },

  persist: {
    enabled: true,
    strategies: [
      {
        key: 'theme',
        storage: localStorage,
      },
    ],
  },
})

export const useAppStore = defineStore('app', {
  state: () => ({
    isDark: false,
    sidebarCollapsed: false
  }),
  
  actions: {
    toggleTheme() {
      this.isDark = !this.isDark
    },
    toggleSidebar() {
      this.sidebarCollapsed = !this.sidebarCollapsed
    }
  },
  
  persist: true
})

export const useToolStore = defineStore('tool', {
  state: () => ({
    customTools: [] as Tool[]
  }),

  actions: {
    addCustomTool(tool: Tool) {
      this.customTools.push({
        ...tool,
        id: `custom-${Date.now()}`,
        isCustom: true
      })
    },

    removeCustomTool(id: string) {
      const index = this.customTools.findIndex(tool => tool.id === id)
      if (index > -1) {
        this.customTools.splice(index, 1)
      }
    },

    editCustomTool(id: string, updates: Partial<Tool>) {
      const tool = this.customTools.find(tool => tool.id === id)
      if (tool) {
        Object.assign(tool, updates)
      }
    }
  },

  persist: {
    enabled: true,
    strategies: [
      {
        key: 'custom-tools',
        storage: localStorage,
      },
    ],
  }
}) 